package com.kcnet.qk.controller.addfriend;

import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import com.kcnet.qk.app.AppContext;
import com.kcnet.qk.constant.Const;
import com.kcnet.qk.constant.StringConst;
import com.kcnet.qk.controller.BasePeopleController;
import com.kcnet.qk.service.AutoAddPeopleService;
import com.kcnet.qk.util.FBStringUtil;
import com.kcnet.qk.util.NodeUtil;
import com.kcnet.qk.util.PreferenceHelper;
import com.kcnet.qk.util.ToastUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import cn.jesse.nativelogger.NLogger;

import static com.kcnet.qk.constant.Const.LIST_VIEW;
import static com.kcnet.qk.util.NodeUtil.findNodeByTextAndPerformClick;

/**
 * Created by Administrator on 2018/4/25 0025.
 * 添加推荐好友
 */

public class AddFriendRecommedCTR extends BasePeopleController {

    public boolean init_imm;

    private static String TAG = "添加推荐好友";

    private static AddFriendRecommedCTR mRongCloudInstance;


    /**
     * 获取RongCloud 实例。
     *
     * @return RongCloud。
     */
    public static AddFriendRecommedCTR getInstance() {
        if (mRongCloudInstance == null) {
            synchronized (AddFriendPhoneCTR.class) {

                if (mRongCloudInstance == null) {
                    mRongCloudInstance = new AddFriendRecommedCTR();
                }
            }
        }

        return mRongCloudInstance;
    }





    @Override
    public void doTask(AccessibilityEvent event) {
        mClassName = (String) event.getClassName();

        mType = event.getEventType();

        if (!initFbMain && mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals(Const.FbUI.FBLauncherUI)) {
            NLogger.d(TAG, "1、点击加好友请求模块");
          //  sleepTimedDefault();
            tagTextClick(FBStringUtil.getString(StringConst.TAB_JHYQQ));
            sleepTimedDefault();
            searchClick();
        }
        else if(!initFbMain && mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals("X.DWZ")){
            NLogger.d(TAG, "3、点击 搜索新好友");
            searchTypeClick(FBStringUtil.getString(StringConst.ADDFRIEND_SEATCH_NEW_FRIEND));
            initFbMain = true;
        }
        else if(mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals("com.facebook.katana.activity.ImmersiveActivity")){
            NLogger.d(TAG, "3、点击 建议");
            tagTextClick(FBStringUtil.getString(StringConst.ADDFRIEND_JIANYI));
            init_imm = true;
        }
        else if ( init_imm && ( mType == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED && mClassName.equals(LIST_VIEW)
                || mType == AccessibilityEvent.TYPE_VIEW_SCROLLED && mClassName.equals(LIST_VIEW))) {
            NLogger.d(TAG, "4、执行 自动添加好友");
            beginAddFriendRecommed();
        }else if(mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals("X.2Bh")){ //弹出对话框自动点击确认
            // mService.execShellCmd("input tap 197 141"); //随便模拟一次点击 消失对话框
            if(findNodeByTextAndPerformClick(getRoot(), FBStringUtil.getString(StringConst.QUEDING))){
            }else {
                findNodeByTextAndPerformClick(getRoot(), FBStringUtil.getString(StringConst.QUEREN));
            }
        }
    }



    /**
     * 執行添加推薦的好友
     */
    private void beginAddFriendRecommed(){
        AccessibilityNodeInfo scrllorNodeInfo = null;
        AccessibilityNodeInfo root = getRoot();
        if (root != null) {
            synchronized (mLock) {
                List<AccessibilityNodeInfo> scrllorNodeInfos = NodeUtil.traverseNodeByClassList(root, LIST_VIEW);
                if (scrllorNodeInfos.size() > 0) {
                    scrllorNodeInfo = scrllorNodeInfos.get(0);
                }
                if (scrllorNodeInfo == null) {
                    ToastUtil.showToast(AppContext.getInstance().getContext(), "scrllorNodeInfo listView null");
                    //  PreferenceHelper.putBoolean(Const.PREF_KEY_STOP_AUTO_FLAG, false);
                    //Log.i("czc", "scrllorNodeInfo 為空 停止任务 ");
                    return;
                }

                for (int q = 0; q < scrllorNodeInfo.getChildCount(); q++) {

                    if (currentAddCount == maxAddCount) {
                        currentAddCount = 0;
                        stopTask();
                        //停止任务
                        break;
                    }

                    AccessibilityNodeInfo as = scrllorNodeInfo.getChild(q);
                    List<AccessibilityNodeInfo> list = as.findAccessibilityNodeInfosByText(FBStringUtil.getString(StringConst.ADDFRIEND_JIAWEIHAOYOU));
                    if (!list.isEmpty()) {
                        for (AccessibilityNodeInfo accessibilityNodeInfo : list) {
                            sleepTimedDefault();
                            currentAddCount++;
                            NLogger.d(TAG, "点击了加为好友" + NodeUtil.performClick(accessibilityNodeInfo));
                            break;
                        }
                    } else {
                        stopTask();
                        break;
                        //errCurrentCoun1t++;
                    }

                    if (q == scrllorNodeInfo.getChildCount() - 1) {
                        if (scrllorNodeInfo != null) {
                            NodeUtil.performScroller(scrllorNodeInfo);
                            break;
                        }
                    }
                }
            }
        }
    }

    public void setParms(int maxAddCount,int minAddTime,int maxAddTime){
       // stopTask();
        EventBus.getDefault().post(new Boolean(true));
        PreferenceHelper.putBoolean(Const.PREF_KEY_STOP_AUTO_FLAG, true);
        this.maxAddCount = maxAddCount;
        this.minAddTime = minAddTime;
        this.maxAddTime = maxAddTime;
        this.currentAddCount = 0;
        this.init_imm = false;
        this.initFbMain = false;
    }

}
